###############################################################################
## 启动时检查版本,如果需要更新版本则先更新, 然后再根据配置,进入对应功能
###############################################################################

from top.wardyan.core.mqtt.mqttconfig import *
from config import Config

import time
import threading



data = {
    "round" : round
}

# if round % 100 == 0 :
#     # 获取当前脚本的最后修改时间
#     last_modified_time = os.path.getmtime(__file__)
#     # 转换为可读格式
#     formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_modified_time))

#     data = {
#         "erpversion":ERP_LASTUPDATE(),
#         "clientversion": formatted_time,
#         "round" : round
#     }

## ERP_LASTUPDATE()
## 10103 请求版本文件清单
## 要等一下再继续, 否则MQTT可能还没连上? 
## 不, 放到异步操作
## 异步线程

# msg : MqttMsg = MqttMsg(str(uuid.uuid4()), ERP_CUSTCODE, current_timestamp(), TOPIC, data, ERP_CUSTCODE, TOPIC, 10103)
# send(msg)



def send_mqtt_message():
    ## 一直等到MQTT连接成功
    cnt = 1
    while not is_connected():  ## 等待MQTT连接成功
        time.sleep(1)
        print("等待MQTT连接成功...次数: %d", cnt)
    msg: MqttMsg = MqttMsg(str(uuid.uuid4()), ERP_CUSTCODE, current_timestamp(), TOPIC, data, ERP_CUSTCODE, TOPIC, 10103)
    send(TOPIC, msg)

# 创建并启动线程
mqtt_thread = threading.Thread(target=send_mqtt_message)
mqtt_thread.start()

## 要不要等下一? 暂时先不等

def chooseapp() :
    ## MAXCOST = Config.getconfig_int("autokill","maxcost")
    project = Config.getconfig_str("project","name")
    location = Config.getconfig_str("project","location")
    ## 按照路径, 动态执行
    ## location是同目录下的一个exe文件
    os.system(location)


    

chooseapp()